<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-17
 * Time: 9:16
 */
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

class Member extends Base
{
    /*会员卡列表展示*/
    public function index()
    {
        if(request::instance()->isPost())
        {
            $name = request::instance()->post('name');
            $aa = preg_match('/[\x{4e00}-\x{9fa5}]/u', $name);/*判断里面有没有汉字*/
            if($aa == 1){/*说明有汉字，那就是暗名称搜*/
                $where['name'] = array('like',"%$name%");
            }else{/*没有就是面料编号*/
                $where['card_num'] = array('like',"%$name%");
            }
        }
        $where['is_del'] = 0;
        $list = DB::name('member')->where($where)->order('id desc')->paginate(15);
        $page = $list->render();
        $list = $list->all();
        return $this->fetch('index',[
            'list'  =>  $list,
            'page'  =>  $page,
        ]);
    }

    /*添加会员卡的方法*/
    public  function create()
    {
        if(request::instance()->isPost())
        {
            $data   =   request::instance()->post();
            if($data['password'] != $data['md5_pas'])
            {
                $this->error('两次密码输入不一致');
                exit();
            }
            $data['money']  =   $data['money']+$data['song_money'];
            $data['md5_pas'] = msc($data['md5_pas']);
            $data['entry_id'] = Session::get('manage_id');
            $data['add_time']   =   time();
            $data['shop_id'] = DB::name('user')->where('id = '.$data['entry_id'])->value('shop_id');
            $cardIsset = DB::name('member')->where('is_del = 0 and status = 1 and card_num ='.$data['card_num'])->value('id');
            if($cardIsset > 0 )
            {
                $this->error('该卡号已存在');
                exit();
            }else{
                $res = DB::name('member')->insert($data);
                if($res)
                {
                    $this->success('添加成功',url('index/index/index',array('con'=>'member','act'=>'index')));
                }else{
                    $this->error('添加失败');
                }
            }
        }
        return $this->fetch('create');
    }
    /*修改和会员卡信息*/
    public function update($id)
    {
        if(request::instance()->isPost())
        {
            $data = request::instance()->post();
            $res = DB::name('member')->where('id = '.$id)->setField($data);
            if($res)
            {
                $this->success('修改信息成功',url('index/index/index',array('con'=>'member','act'=>'index')));
            }
        }
        $data = DB::name('member')->where('is_del = 0 and id ='.$id)->find();
        return $this->fetch('create',[
            'data'      =>  $data,
        ]);
    }
    /*修改会员卡状态*/
    public  function changeStatus()
    {
        if(request::instance()->isPost())
        {
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_changeStatus');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $id = Request::instance()->post('id');
                $data['status'] = Request::instance()->post('status');
                $result = DB::name('member')->where('id ='.$id)->setField($data);
                if($result){
                    $res['sta'] = '1';
                    $res['msg'] = '修改成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '修改失败';
                }
            }
            return json($res);
        }
    }
    /*删除会员卡信息  只修改状态 不删除数据*/
    public function deleteMember()
    {
        if(request::instance()->isPost())
        {
            $id = Request::instance()->post('id');
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_delMember');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $del = DB::name('member')->where('id = '.$id)->setField('is_del',1);
                if($del){
                    $res['sta'] = '1';
                    $res['msg'] = '删除成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '删除失败';
                }
            }
            return json($res);
        }
    }

    /*修改会员卡密码*/
    public  function updatePass()
    {
        if(request::instance()->isPost())
        {
            $card_num   =   request::instance()->post('card_num');
            $password   =   request::instance()->post('password');
            $md5_pas    =   request::instance()->post('md5_pas');
            /*先判断两次密码输入是否一致*/
            if($password != $md5_pas)
            {
                $this->error('密码不一致');
                exit();
            }else{
                DB::startTrans();
                try{
                    $data['card_num']   =   $card_num;
                    $data['new_pass']   =   $update['password'] =   $password;
                    $data['new_md5pass']=   $update['md5_pas']  =   msc($md5_pas);
                    $data['add_time']   =   time();
                    $data['entry_id']   =   Session::get('manage_id');
                    $data['last_pass']  =   DB::name('member')->where('is_del = 0 and card_num ='.$card_num)->value('password');
                    $res = DB::name('member')->where('is_del = 0 and card_num ='.$card_num)->setField($update);
                    $insert =   DB::name('member_updatepass')->insert($data);
                    DB::commit();
                }catch(\Exception $e){
                    Db::rollback();
                    $this->error('密码修改失败');
                }
                $this->success('修改密码成功',url('index/index/index',array('con'=>'member','act'=>'index')));
            }
        }
        return $this->fetch('update_pass');
    }
    /*查看会员卡信息*/
    public function checkInfo()
    {
        if(request::instance()->isPost())
        {
            $where['card_num']   =   request::instance()->post('card_num');
            $where['phone']      =   request::instance()->post('phone');
            $where['id_number']  =   request::instance()->post('id_number');
            $where['is_del']     =   0;
            $where['status']     =   1;
            $data = DB::name('member')->where($where)->find();
            if($data){
                $res['sta'] =   1;
                $res['msg'] =   'success';
            }else{
                $res['sta'] =   0;
                $res['msg'] =   'error';
            }
            return json($res);
        }
    }
    /*消费的时候 先查询下会员卡的信息*/
    public function getMemberInfo()
    {
        if(request::instance()->isPost())
        {
            $card_num   =   request::instance()->post('card_num');
            $data       =   DB::name('member')->field("status,money,name,DATE_FORMAT(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s'), '%Y-%m-%d') AS add_time")->where('is_del = 0 and card_num = '.$card_num)->find();
            if($data)/*卡号存在*/
            {
                if($data['status'] ==1)
                {
                    $res['sta'] =   1;
                    $res['msg'] =   '正常用户';
                    $res['money']   =   $data['money'];
                    $res['name']    =   $data['name'];
                    $res['add_time']=   $data['add_time'];
                }else{
                    $res['sta'] =   2;
                    $res['msg'] =   '该卡已被冻结';
                }
            }else{
                $res['sta'] =   -1;
                $res['msg'] =   '该卡不存在';
            }
            return json($res);
        }
    }
    /*验证用户的密码和数据库的是否一样*/
    public function checkPassword()
    {
        if(request::instance()->isPost())
        {
            $where['md5_pas']   =   msc(request::instance()->post('password'));
            $where['card_num']   =   request::instance()->post('card_num');
            $where['status']     =   1;
            $where['is_del']     =   0;
            $data       =   DB::name('member')->where($where)->value('id');
            if($data > 0)
            {
                $res['sta'] =   1;
                $res['msg'] =   '正确';
            }else{
                $res['sta'] =   0;
                $res['msg'] =   '密码错误';
            }
            return json($res);
        }
    }
/*====================================================定制消费记录开始的地方===========================================================*/
    /*定制消费记录*/
    public function onlineRow()
    {
        /*通过订单号进行查询*/
        if(request::instance()->isPost())
        {
            $name   =   request::instance()->post('name');
            $where['a.card_num']   =   $name;
        }
        $where['a.is_del'] = 0;
        $list   =   DB::name('member_online')
                        ->alias('a')
                        ->join('category b','a.category = b.id')
                        ->join('user c','a.entry_id = c.id')
                        ->join('shop d','a.shop_id = d.id')
                        ->join('member e','a.card_num = e.card_num')
                        ->field('a.id,a.card_num,e.name as card_name,b.title as category,c.truename,d.title as shop,a.order_num,a.money,a.add_time')
                        ->where($where)
                        ->order('a.id desc')
                        ->paginate(15);
        $page   =   $list->render();
        $list   =   $list->all();
        foreach($list as $k=>$v)
        {
            $list[$k]['cust_name']   =   DB::name('customer')->alias('a')->join('order b','a.id = b.cust_id')->where('b.order_num = '.$v['order_num'])->value('a.name');
        }
        return $this->fetch('online_row',[
            'list'      =>  $list,
            'page'      =>  $page,
        ]);
    }
    /*删除定制消费记录  只修改状态 不删除数据*/
    public  function delOnlineRow()
    {
        if(request::instance()->isPost())
        {
            $id = Request::instance()->post('id');
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_delOnlineRow');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $del = DB::name('member_online')->where('id = '.$id)->setField('is_del',1);
                if($del){
                    $res['sta'] = '1';
                    $res['msg'] = '删除成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '删除失败';
                }
            }
            return json($res);
        }
    }
/*====================================================定制消费记录结束的地方  成衣消费记录开始的地方=======================================*/
    /*成衣消费记录*/
    public function buyRow()
    {
       if(request::instance()->isPost())
       {
           $name    =   request::instance()->post('name');
           $where['a.card_num']     =   $name;
       }
        $where['a.is_del']  =   0;
        $list   =   DB::name('member_buyrow')
                        ->alias('a')
                        ->join('clothes_code b','a.code = b.code')
                        ->join('member c','a.card_num = c.card_num')
                        ->join('shop d','a.shop_id = d.id')
                        ->join('user e','a.entry_id = e.id')
                        ->field('a.id,c.name,a.card_num,a.code,b.name as clothes_name,b.ml_num,b.category,b.color,b.size,d.title as shop,a.money,a.add_time,e.truename as entry')
                        ->where($where)
                        ->order('a.id desc')
                        ->paginate(15);
        $page   =   $list->render();
        $list   =   $list->all();
        foreach($list as $k=>$v)
        {
            $list[$k]['category']   =   DB::name('clothes_category')->where('id = '.$v['category'])->value('value');
            $list[$k]['color']	    =   DB::name('clothes_color')->where('id = ' .$v['color'])->value('value');
            $list[$k]['size']       =   DB::name('clothes_size')->where('id = '.$v['size'])->value('value');
        }
        return $this->fetch('buy_row',[
            'list'      =>  $list,
            'page'      =>  $page,
        ]);
    }
    /*成衣消费记录删除的方法 只修改状态 不删除数据*/
    public  function delBuyRow()
    {
        if(request::instance()->isPost())
        {
            $id = Request::instance()->post('id');
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_delBuyRow');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $del = DB::name('member_buyrow')->where('id = '.$id)->setField('is_del',1);
                if($del){
                    $res['sta'] = '1';
                    $res['msg'] = '删除成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '删除失败';
                }
            }
            return json($res);
        }
    }
/*===============================================成衣消费记录结束的地方  会员卡充值 补办方法的开始=========================================*/
    /*会员卡充值的方法*/
    public function recharge()
    {
        if(request::instance()->isPost())
        {
            $data   =   request::instance()->post();
            $data['entry_id']   =   Session::get('manage_id');
            $data['shop_id']    =   DB::name('user')->where('id = '.$data['entry_id'])->value('shop_id');
            $data['add_time']   =   time();
            DB::startTrans();
            try{
                $order['money'] =   $data['money'] + $data['song_money'];
                $order['song_money']=   $data['song_money'];
                $order['jifen'] =   $data['jifen'];
                $money          =   DB::name('member')->where('card_num = '.$data['card_num'])->setInc('money',$order['money']);
                $song_money     =   DB::name('member')->where('card_num = '.$data['card_num'])->setInc('song_money',$order['song_money']);
                $jifen          =   DB::name('member')->where('card_num = '.$data['card_num'])->setInc('jifen',$order['jifen']);
                $rechargeRow    =   DB::name('member_recharge')->insert($data);
                DB::commit();
            }catch (\Exception $e){
                DB::rollback();
                $this->error('充值失败,请重试');
            }
            $this->success('充值成功',url('index/index/index',array('con'=>'member','act'=>'index')));
        }
        return $this->fetch('recharge');
    }
    /*会员卡充值记录列表的方法*/
    public function rechargeList()
    {
        if(request::instance()->isPost())
        {
            $name   =   request::instance()->post('name');
            $where['a.card_num']    =   $name;
        }
        $where['a.is_del']  =   0;
        $list   =   DB::name('member_recharge')
                        ->alias('a')
                        ->join('member b','a.card_num = b.card_num')
                        ->join('shop c','a.shop_id = c.id')
                        ->join('user d','a.entry_id = d.id')
                        ->field('b.name as card_name,a.id,a.card_num,a.money,a.song_money,a.jifen,a.add_time,c.title as shop,d.truename as entry')
                        ->where($where)
                        ->order('id desc')
                        ->paginate(15);
        $page       =   $list->render();
        $list       =   $list->all();
        return $this->fetch('recharge_list',[
            'list'      =>  $list,
            'page'      =>  $page,
        ]);
    }
    /*删除会员卡充值记录*/
    public  function delRechargeRow()
    {
        if(request::instance()->isPost())
        {
            $id = Request::instance()->post('id');
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_delRechargeRow');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $del = DB::name('member_recharge')->where('id = '.$id)->setField('is_del',1);
                if($del){
                    $res['sta'] = '1';
                    $res['msg'] = '删除成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '删除失败';
                }
            }
            return json($res);
        }
    }
/*===============================================会员卡充值方法结束   补办会员卡的方法开始===============================================*/
    /*补办会员卡的方法*/
    public  function  reissue()
    {
        if(request::instance()->isPost())
        {
            $data   =   request::instance()->post();
            /*先判断下两次的密码是否一样*/
            if($data['password'] != $data['md5_pas'])
            {
                $this->error('两次密码输入不一致');
                exit();
            }
            /*开启数据库的回滚  先冻结之前的卡然后 再查询出姓名和卡上余额 再进行信息转移*/
            DB::startTrans();
            try{
                /*先根据条件冻结旧卡*/
                $disable    =   DB::name('member')->where('card_num = '.$data['card_num'].' and phone = '.$data['phone'].' and id_number = '.$data['id_number'])->setField('status',0);
                $oldCard    =   DB::name('member')->field('name,sex,money,song_money,jifen')->where('card_num = '.$data['card_num'].' and phone = '.$data['phone'].' and id_number = '.$data['id_number'])->find();
                $oldCard['phone']   =   $data['phone'];
                $oldCard['id_number']=  $data['id_number'];
                $oldCard['card_num']=   $data['new_num'];
                $oldCard['password']=   $data['password'];
                $oldCard['md5_pas'] =   msc($data['md5_pas']);
                $oldCard['entry_id']=   Session::get('manage_id');
                $oldCard['shop_id'] =   DB::name('user')->where('id = '.$oldCard['entry_id'])->value('shop_id');
                $oldCard['add_time']=   time();
                $new        =   DB::name('member')->insert($oldCard);
                $reissue['old_card']    =   $data['card_num'];
                $reissue['new_card']    =   $data['new_num'];
                $reissue['new_pass']    =   $data['password'];
                $reissue['new_md5pass'] =   $oldCard['md5_pas'];
                $reissue['entry_id']    =   $oldCard['entry_id'];
                $reissue['shop_id']     =   $oldCard['shop_id'];
                $reissue['add_time']    =   time();
                $reissueRow =   DB::name('member_reissue')->insert($reissue);
                DB::commit();
            }catch (\Exception $e)
            {
                DB::rollback();
                $this->error('补卡失败，请重新办理');
            }
            $this->success('补卡成功',url('index/index/index'));
        }
        return $this->fetch('reissue');
    }
    /*查看会员卡信息 看看该卡是否存在 不管状态*/
    public function checkCardInfo()
    {
        if(request::instance()->isPost())
        {
            $where['card_num']   =   request::instance()->post('card_num');
            $where['phone']      =   request::instance()->post('phone');
            $where['id_number']  =   request::instance()->post('id_number');
            $where['is_del']     =   0;
            $data = DB::name('member')->where($where)->find();
            if($data){
                $res['sta'] =   1;
                $res['msg'] =   'success';
            }else{
                $res['sta'] =   0;
                $res['msg'] =   'error';
            }
            return json($res);
        }
    }
    /*补卡信息列表*/
    public function reissueList()
    {
        if(request::instance()->isPost())
        {
            $name   =   request::instance()->post('name');
            $where['a.new_card']    =   $name;
        }
        $where['a.is_del']  =   0;
        $list   =   DB::name('member_reissue')
                        ->alias('a')
                        ->join('member b','a.new_card = b.card_num')
                        ->join('user c','a.entry_id = c.id')
                        ->join('shop d','a.shop_id = d.id')
                        ->field('a.id,a.old_card,a.new_card,a.add_time,b.name,b.sex,b.money,b.song_money,b.jifen,c.truename as entry,d.title as shop')
                        ->where($where)
                        ->order('a.id desc')
                        ->paginate(15);
        $page   =   $list->render();
        $list   =   $list->all();
        return $this->fetch('reissue_list',[
            'list'      =>  $list,
            'page'      =>  $page,
        ]);
    }
    /*删除补卡信息 只修改状态 不删除数据*/
    public  function delReissueRow()
    {
        if(request::instance()->isPost())
        {
            $id = Request::instance()->post('id');
            /*判断权限*/
            $authmenu = new \think\Authmenu();
            $uid = Session::get('manage_id');
            $auth=$authmenu->check($uid,'member_delReissueRow');
            if($auth !=true){//没有删除菜单的权限
                $res['sta'] = '-1';
                $res['msg'] = '权限不足';
            }else{
                $del = DB::name('member_reissue')->where('id = '.$id)->setField('is_del',1);
                if($del){
                    $res['sta'] = '1';
                    $res['msg'] = '删除成功';
                }else{
                    $res['sta'] = '0';
                    $res['msg'] = '删除失败';
                }
            }
            return json($res);
        }
    }
}
